package njust.edu.service.impl;

import njust.edu.dao.RoleDao;
import njust.edu.dao.UserDao;
import njust.edu.dao.impl.RoleDaoImpl;
import njust.edu.dao.impl.UserDaoImpl;
import njust.edu.entity.Role;
import njust.edu.entity.User;
import njust.edu.service.UserService;
import njust.edu.web.Page;
import njust.edu.web.SalaryOpe;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
//注意这里的加密要取消注释
public class UserServiceImpl implements UserService {
    UserDao userdao = new UserDaoImpl();
    RoleDao roleDao = new RoleDaoImpl();

    @Override
    public User getUserById(Long id) {
        User user = userdao.findById(id);
        user.setRole(roleDao.findById(user.getType()));
        return user;
    }

    @Override
    public User getUserByName(String name) {
        User user = userdao.findByName(name);

        return user;
    }

    @Override
    public Page<User> getPage(int pageNum) {
        Page<User> page = userdao.getPage(pageNum);
        for (User u : page.getList()) {
            u.setRole(roleDao.findById(u.getType()));
        }
        return page;
    }

    @Override
    public User edit(Long id, String password) {
        User user = userdao.findById(id);
        user.setPassword(password);
        userdao.update(user);
        return userdao.findById(id);
    }

    @Override
    public void delUser(Long id) {
        userdao.delete(id);
    }

    @Override
    public List<Role> getRoleList() {
        return roleDao.getList();
    }

    @Override
    public void addUser(String name, String password, String tell) {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setTell(tell);
        user.setType(1);
        Role rol =new Role(1,"客户");
        user.setRole(rol);
        List<User> lUser = new ArrayList<>();
        lUser=getUserList();
        Long id = lUser.stream().max(Comparator.comparing(User::getId)).get().getId();
        user.setId(++id);
        userdao.add(user );

    }

    @Override
    public void editUser(Long id, String name, Integer type, String tell) {
        User user = userdao.findById(id);
        user.setType(type);
        user.setName(name);
        user.setTell(tell);
        userdao.update(user);
    }

    @Override
    public void editUserForPassword(Long id) {
        User user = userdao.findById(id);
        user.setPassword("12345");
        userdao.update(user);
    }

    @Override
    public List<User> getUserList() {
        return userdao.getList();
    }

    @Override
    public boolean checkUserPassword(String name, String password) {
        User user = userdao.findByName(name);

        if(user.getName() == null){

            return false;
        }

        if(user.getPassword().equals(password)){
            return true;

        }

        //TODO Auto-generated catch block
//        if(user.getPassword().equals(MD5Util.string2MD5(password))){
//            return true;
//        }
        return false;
    }

    @Override
    public SalaryOpe getSalaryOpe(String date, Integer pageNo) {
        //2015-06-01;
        String yearStr = date.substring(0,4);
        String monStr = date.substring(5,7);
        String dayStr =date.substring(8,10);

//        System.out.println(yearStr);
//        System.out.println(monStr);
        int year= Integer.parseInt(yearStr);
        int mon = Integer.parseInt(monStr);
        int day = Integer.parseInt(dayStr);

        // TODO: 2021/9/27 月份第一天
        Calendar ca = Calendar.getInstance();

        ca.set(Calendar.YEAR, year);

        ca.set(Calendar.MONTH, mon-1);

        ca.set(Calendar.DAY_OF_MONTH, 1);

        ca.set(Calendar.HOUR_OF_DAY, 0);

        ca.set(Calendar.MINUTE,0);

        ca.set(Calendar.SECOND, 0);

        java.util.Date date1 = ca.getTime();

        //System.out.println(date1.getTime());

        // TODO: 2021/9/27 月份最后一天
        ca.set(Calendar.YEAR, 2021);
        ca.set(Calendar.DAY_OF_MONTH, 31);
        java.util.Date date2 = ca.getTime();

        // TODO: 2021/9/27 被选中的当天
        ca.set(Calendar.YEAR, year);

        ca.set(Calendar.MONTH, mon-1);

        ca.set(Calendar.DAY_OF_MONTH, day);

        java.util.Date date3 = ca.getTime();

        SalaryOpe salaryOpe = new SalaryOpe(pageNo,new Date(date1.getTime()),new Date(date2.getTime()),new Date(date3.getTime()));

        //System.out.println(salaryOpe.getUserSalaryList());
        return salaryOpe;
    }


}
